<?php
/**
 * Created by PhpStorm.
 * User: Liulibao
 * Date: 2019/10/11
 * Time: 22:05
 */

namespace app\common\model;


use think\Model;

class InquiryHistoryModel extends Model
{
    protected $table = 'ds_inquiry_history';

    public $page_info;

    protected $pageSize = 10;

    /**
     * @description: 获取列表  @MrLiu
     * @param array $condition
     * @param $page
     * @param string $order
     * @return false|\PDOStatement|string|\think\Collection
     * @internal param bool $isArray
     */
    public function getInquiryHistoryList($condition = [], $page = 0, $order = 'history.id desc')
    {
        $condition['history.is_del'] = 0;

               $result = db('inquiry_history')->alias('history');
//        var_dump($condition);
//        die();
//        if (!empty($condition) && is_array($condition)) {
//            $result = $result->where($condition);
//        }
        if ($page) {
            $result = $result
                ->field('history.*,goodscommon.goods_name,goodscommon.gc_name,inquiries.inquiry_name,inquiry_station.station_name')
                ->join('goodscommon', 'goodscommon.goods_commonid=history.goods_id', 'LEFT')
                ->join('inquiries', 'inquiries.id=history.inquiry_id', 'LEFT')
                ->join('inquiry_station', 'inquiry_station.station_id=history.station_id', 'LEFT')
                ->order($order)
                ->where($condition)
                ->paginate($page, false, ['query' => request()->param()]);

            $this->page_info = $result;
            $result = $result->order($order)->paginate($page, false, ['query' => request()->param()]);

            $this->page_info = $result;

            return $result->items();
        } else {
            return $result->order($order)->select();
        }
    }

    /**
     * @description:   @MrLiu
     * @param array $condition
     * @param $page
     * @param string $field
     * @return array
     */
    public function getList($condition = [])
    {
        $condition['is_del'] = 0;
        $conditions = !empty($condition) ? $condition : array('id' => array('gt', '0'));
        return $this->where($conditions)
            ->paginate($this->pageSize)
            ->toArray();
    }

    /**
     * @description: 获取某一条数据  @MrLiu
     * @param $id
     * @return array|bool|false|\PDOStatement|string|Model
     */
    public function getOneById($id)
    {
        if (intval($id) <= 0) {
            return false;
        }
        return $this->where(array('id' => $id, 'is_del' => 0))->find();
    }

    /**
     * @description: 根据条件查询获取某一条数据 @MrLiu
     * @param array $condition
     * @return array|bool|false|\PDOStatement|string|Model
     */
    public function getOne($condition = [])
    {
        if (empty($condition)) {
            return false;
        }

        $condition['is_del'] = 0;

        return $this->where($condition)->find();
    }

    /**
     * @description: 新增数据  @MrLiu
     * @param $data
     * @return int|string
     */
    public function add($data)
    {
        $data['created_at'] = date('Y-m-d H:i:s', time());
        return $this->insertGetId($data);
    }

    /**
     * @description: 更新信息  @MrLiu
     * @param $data
     * @param $ac_id
     * @return $this
     */
    public function edit($data,$id)
    {
        return $this->where('id',$id)->update($data);
    }

    /**
     * @description: 删除分类   @MrLiu
     * @param $id
     * @return $this
     */
    public function del($id)
    {
        return $this->where('id', $id)->update(array('is_del' => 1));
    }

}